﻿#include "edit.h"
#include<qinputdialog.h>
#include<QMimeData.h>
void myNotePad::undoActionSlot() {//撤销实现
	mainText->undo();
}

void myNotePad::cutActionSlot() {//剪切实现
	mainText->cut();
}

void myNotePad::copyActionSlot() {//复制实现
	mainText->copy();
}

void myNotePad::pasteActionSlot() {//粘贴实现
	mainText->paste();
}

void myNotePad::copyAsPlainTextActionSlot() {//复制为纯文本实现
	//设置文本到剪切板上
	QClipboard* clipboard = QGuiApplication::clipboard();
	clipboard->setText(mainText->toPlainText(), QClipboard::Clipboard());
}

void myNotePad::copyAsMarkdownSlot() {//复制为Markdown实现
	QString markdownText= mainText->toPlainText();

	// 将文本转换为Markdown格式
	markdownText.prepend("```markdown\n");
	markdownText.append("\n```");

	// 将Markdown格式的文本复制到剪贴板
	QClipboard* clipboard = QGuiApplication::clipboard();
	clipboard->setText(markdownText, QClipboard::Clipboard);
}

void myNotePad::copyAsHTMLSlot() {//复制为HTML实现
	QString selectedText = mainText->textCursor().selectedText();
	if (!selectedText.isEmpty()) {
		// 创建MIME数据对象
		QMimeData* mimeData = new QMimeData;
		mimeData->setHtml(selectedText);

		// 将MIME数据设置到剪贴板中
		QClipboard* clipboard = QApplication::clipboard();
		clipboard->setMimeData(mimeData);
	}

}

void myNotePad::emojiAndSymbolsActionSlot() {//查找和替代实现
	QString searchText = QInputDialog::getText(this, "查找", "查找的文本");
	if (!searchText.isEmpty()) {
		QTextCursor cursor = mainText->textCursor();
		QTextDocument::FindFlags flags;
		//if (QApplication::keyboardModifiers() & Qt::ShiftModifier) 
			flags = QTextDocument::FindBackward;
		bool found = cursor.movePosition(QTextCursor::Start);
		while (found) {
			found = mainText->find(searchText, flags);
			if (found) {
				QTextCharFormat format;
				cursor.mergeCharFormat(format);
			}
		}
	}
}
